package cn.sykj.car.service.impl;

import cn.sykj.car.common.StatusCode;
import cn.sykj.car.entity.Dictionary;
import cn.sykj.car.exception.CommonException;
import cn.sykj.car.exception.DictionaryException;
import cn.sykj.car.exception.ParameterInvalidException;
import cn.sykj.car.mapper.DictionaryMapper;
import cn.sykj.car.service.IDictionaryService;
import cn.sykj.car.utils.Utils;
import cn.sykj.car.vo.DictionaryVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 字典表 服务实现类
 * </p>
 *
 * @author Yixu
 * @since 2018-12-06
 */
@Service
public class DictionaryServiceImpl extends ServiceImpl<DictionaryMapper, Dictionary> implements IDictionaryService {

    /**
     * 新增字典表
     *
     * @param dictionary
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public Boolean addDictionary(Dictionary dictionary) throws Exception {
        //验证参数是否为空
        if(Utils.isEmpty(dictionary)){
            throw  new ParameterInvalidException();
        }
        //验证字段是否为空
        if(Utils.isEmpty(dictionary.getDictGroup())){
            throw new DictionaryException("","字典簇不能为空");
        }

        //验证分类code是否为空
        if (Utils.isEmpty(dictionary.getCode())){
            throw new DictionaryException("","字典码不能为空");
        }
        //验证分类名是否为空
        if (Utils.isEmpty(dictionary.getDictValue())){
            throw new DictionaryException("","字典值不能为空");
        }
        //验证分类排序是否为空
        if (Utils.isEmpty(dictionary.getSeq())){
            throw new CommonException(StatusCode.GOODS_CATEGORY_SEQ_NOT_NULL.getCode(),StatusCode.GOODS_CATEGORY_SEQ_NOT_NULL.getMsg());
        }

        // 验证字典码是否存在
        QueryWrapper<Dictionary> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(Dictionary.CODE,dictionary.getCode());
        queryWrapper.eq(Dictionary.DICT_GROUP,dictionary.getDictGroup());
        List<Dictionary> list = this.list(queryWrapper);
        if(Utils.notEmpty(list) && list.size() > 0){
            throw new DictionaryException("","字典码不能重复！");
        }

        boolean ret=this.save(dictionary);
        return ret;
    }

    /**
     * 修改字典表
     *
     * @param dictionary
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public Boolean updateDictionary(Dictionary dictionary) throws Exception{
        //验证参数是否为空或者id为空
        if(Utils.isEmpty(dictionary)||Utils.isEmpty(dictionary.getId())){
            throw new ParameterInvalidException();
        }
        //判断原始记录是否存在
        Dictionary condition = this.getById(dictionary.getId());
        if(Utils.isEmpty(condition)){
            throw new DictionaryException("","字典数据不存在");
        }

        //验证字典码是否为空
        if (Utils.isEmpty(dictionary.getCode())) {
            throw new DictionaryException("", "字典码不能为空");
        }
        //验证字典值是否为空
        if (Utils.isEmpty(dictionary.getDictValue())){
            throw new DictionaryException("","字典值不能为空");
        }
        //验证分类排序是否为空
        if (Utils.isEmpty(dictionary.getSeq())){
            throw new CommonException(StatusCode.GOODS_CATEGORY_SEQ_NOT_NULL.getCode(),StatusCode.GOODS_CATEGORY_SEQ_NOT_NULL.getMsg());
        }

        // 验证簇
        if(Utils.isEmpty(dictionary.getDictGroup())){
            throw new DictionaryException("","字典簇不能为空");
        }

        if(!condition.getDictGroup().equals(dictionary.getDictGroup())){
            throw new DictionaryException("","字典簇不能修改");
        }

        // 验证字典码是否存在（除本身外）
        QueryWrapper<Dictionary> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(Dictionary.CODE,dictionary.getCode());
        queryWrapper.eq(Dictionary.DICT_GROUP,dictionary.getDictGroup());
        queryWrapper.ne(Dictionary.ID,dictionary.getId());
        List<Dictionary> list = this.list(queryWrapper);
        if(Utils.notEmpty(list) && list.size() > 0){
            throw new DictionaryException("","字典码不能重复！");
        }

        // 把要更新的对象中的属性，覆盖到已有对象中
        boolean updateById = baseMapper.updateDictionary(dictionary) > 0;
        return updateById;
    }

    /**
     * 删除字典表
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public Boolean deleteDictionary(Integer id) throws Exception{
        //验证参数是否为空
        if (Utils.isEmpty(id)){
            throw new ParameterInvalidException();
        }
        Dictionary dictionary=this.getById(id);
        if (Utils.isEmpty(dictionary)){
            throw new DictionaryException("","字典不存在");
        }

        boolean a=this.removeById(id);

        return a;
    }

    /**
     * 查询字典表
     *
     * @param dictionary
     * @return
     * @throws Exception
     */
    @Override
    public IPage<DictionaryVO> pageDictionary(IPage<DictionaryVO> page, Dictionary dictionary) throws Exception{
        //判断参数是否为空
        if (Utils.isEmpty(dictionary)) {
            throw new ParameterInvalidException();
        }
        List<DictionaryVO> list = baseMapper.pageDictionary(page, dictionary);

        page.setRecords(list);
        return page;
    }

    /**
     * 下拉
     *
     * @param dictGroup
     * @return
     * @throws Exception
     */
    @Override
    public List<Dictionary> getListByGroup(String dictGroup) throws Exception {
        QueryWrapper<Dictionary> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(Dictionary.DICT_GROUP,dictGroup);
        queryWrapper.orderByAsc(Dictionary.SEQ);
        List<Dictionary> list = this.list(queryWrapper);
        return list;
    }


}
